<template>
  <div class="page-content">
    <i-table v-model:page="pageInfo" :listData="dataList" v-bind="ContentTableConfig" :listCount="Counter">
      <!-- header插槽 -->
      <template #headerHandler>
        <el-button type="primary" size="medium" @click="handleAddClick">新建数据</el-button>
      </template>
      <!-- 公共列插槽 -->
      <template #createAt="scope">
        <span>{{ $filters.formatTime(scope.row.createAt) }}</span>
      </template>
      <template #updateAt="scope">
        <span>{{ $filters.formatTime(scope.row.updateAt) }}</span>
      </template>
      <template #handler="scope">
        <div class="handle-btns">
          <el-button v-if="isUpdate" icon="el-icon-edit" size="mini" type="text" @click="handleEditClick(scope.row)">
            编辑
          </el-button>
          <el-button v-if="isDelete" icon="el-icon-delete" size="mini" type="text" @click="handleDeleteClick(scope.row)"
            >删除</el-button
          >
        </div>
      </template>
      <!-- 各个组件传入动态插槽 -->
      <template v-for="item in otherPropSlots" :key="item.prop" #[item.slotName]="scope">
        <template v-if="item.slotName">
          <slot :name="item.slotName" :row="scope.row"></slot>
        </template>
      </template>
    </i-table>
  </div>
</template>
<script lang="ts">
import { defineComponent, computed, ref, watch } from 'vue'
import ITable from '@/base-ui/table/index'
import { useStore } from '@/store'
import { usePermission } from '@/hooks/usePermission'
export default defineComponent({
  components: { ITable },
  props: {
    ContentTableConfig: {
      type: Object,
      required: true
    },
    pageName: {
      type: String,
      required: true
    }
  },
  emits: ['newBtnClick', 'editBtnClick'],
  setup(props, { emit }) {
    // 获取页面按钮操作权限
    const isCreate = usePermission(props.pageName, 'create')
    const isUpdate = usePermission(props.pageName, 'update')
    const isDelete = usePermission(props.pageName, 'delete')
    const isQuery = usePermission(props.pageName, 'query')
    // 分页参数
    const pageInfo = ref({ currentPage: 1, pageSize: 10 })
    watch(pageInfo, () => getPageData())

    const store = useStore()
    // 发送网络请求
    const getPageData = (queryInfo: any = {}) => {
      console.log('xxx')
      console.log(isQuery)

      if (!isQuery) return
      store.dispatch('system/getPageListAction', {
        pageName: props.pageName,
        query: {
          offset: (pageInfo.value.currentPage - 1) * pageInfo.value.pageSize,
          size: pageInfo.value.pageSize,
          ...queryInfo
        }
      })
    }
    getPageData()
    const dataList = computed(() => store.getters['system/pageListData'](props.pageName))
    const Counter = computed(() => store.getters['system/pageListCount'](props.pageName))
    // 获取其他的动态插槽名称
    const otherPropSlots = props.ContentTableConfig?.propList.filter((item: any) => {
      if (item.slotName === 'createAt') return false
      if (item.slotName === 'updateAt') return false
      if (item.slotName === 'handler') return false
      return true
    })
    // 删除
    const handleDeleteClick = (item: any) => {
      store.dispatch('system/deletePageDataAction', {
        pageName: props.pageName,
        id: item.id
      })
    }
    // 新增
    const handleAddClick = () => {
      emit('newBtnClick')
    }
    const handleEditClick = (item: any) => {
      emit('editBtnClick', item)
    }
    return {
      pageInfo,
      dataList, // 列表数据
      getPageData,
      Counter, // 数据总计
      otherPropSlots,
      isCreate,
      isUpdate,
      isDelete,
      handleDeleteClick,
      handleAddClick,
      handleEditClick
    }
  }
})
</script>
<style lang="less" scoped></style>
